package com.zs.demo.controller.system;

import com.baomidou.mybatisplus.plugins.Page;
import com.zs.demo.common.log.SysLogAop;
import com.zs.demo.common.response.CommonStatus;
import com.zs.demo.common.response.IResponseStatus;
import com.zs.demo.pojo.dto.system.user.FindUserDTO;
import com.zs.demo.pojo.dto.system.user.ResetPasswordDTO;
import com.zs.demo.pojo.dto.system.user.UserAddDTO;
import com.zs.demo.pojo.dto.system.user.UserUpdateDTO;
import com.zs.demo.pojo.entity.system.SysUser;
import com.zs.demo.pojo.vo.system.SysUserVO;
import com.zs.demo.service.system.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/* @Desc: 用户管理控制器
 * @author:zhengs  
 * @Time: 2018/11/20 11:25
 * @Copyright: © zhengs 版权所有  
 * @Warning: 本内容仅限于本公司内部传阅,禁止外泄或用于其它商业目的
 */
@RestController
@RequestMapping(value = {"/system/user"})
@Api(tags = {"用户管理"})
public class UserController {

    @Autowired
    private SysUserService userService;

    @PostMapping(value = {"/list"})
    @ApiOperation(value = "分页获取用户数据")
    @SysLogAop("分页获取用户数据")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public Page<SysUserVO> get(@RequestBody @Validated @ApiParam(value = "用户获取过滤条件") FindUserDTO findUserDTO){
        return userService.getAllUserBySplitPage(findUserDTO);
    }

    @PostMapping(value = {"/get/id/{id}"})
    @ApiOperation(value = "根据ID获取用户信息")
    @SysLogAop("根据ID获取用户信息")
    public SysUser getById(@PathVariable("id") @ApiParam(value = "用户ID") String id){
        return userService.findUserById(id,true);
    }

    @PostMapping(value = {"/lock/{id}"})
    @ApiOperation(value = "锁定用户")
    @SysLogAop("锁定用户")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public IResponseStatus lock(@PathVariable("id") @ApiParam(value = "用户标识ID") String id){
        userService.statusChange(id, "0");
        return CommonStatus.OK;
    }

    @PostMapping(value = {"/unlock/{id}"})
    @ApiOperation(value = "解锁用户")
    @SysLogAop("解锁用户")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public IResponseStatus unlock(@PathVariable("id") @ApiParam(value = "用户标识ID") String id){
        userService.statusChange(id, "1");
        return CommonStatus.OK;
    }

    @PostMapping(value = {"/remove/{id}"})
    @ApiOperation(value = "删除用户")
    @SysLogAop("删除用户")
    public IResponseStatus remove(@PathVariable("id") @ApiParam(value = "用户标识ID") String id){
        userService.removeUser(id);
        return CommonStatus.OK;
    }

    @PostMapping(value = {"/add"})
    @ApiOperation(value = "添加用户")
    @SysLogAop("添加用户")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public IResponseStatus add(@RequestBody @Validated @ApiParam(value = "用户数据") UserAddDTO addDTO){
        userService.add(addDTO);
        return CommonStatus.OK;
    }

    @PostMapping(value = {"/update/{id}"})
    @ApiOperation(value = "更新用户")
    @SysLogAop("更新用户")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public IResponseStatus update(@PathVariable("id") @ApiParam(value = "用户标识ID") String id,
                               @RequestBody @Validated @ApiParam(value = "用户数据") UserUpdateDTO updateDTO){
        userService.update(id,updateDTO);
        return CommonStatus.OK;
    }

    @PostMapping(value = {"/reset-password"})
    @ApiOperation(value = "重置密码")
    @SysLogAop("重置密码")
    @ApiImplicitParam(paramType = "header",name = "Authorization",value = "身份认证Token")
    public IResponseStatus resetPassword(@RequestBody
                                           @Validated @ApiParam(value = "用户及密码数据") ResetPasswordDTO dto){
        userService.resetPassword(dto);
        return CommonStatus.OK;
    }

}